package com.xqls.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.DictionaryBean;
import com.xqls.bean.LevelTimeBean;
import com.xqls.bean.MessageBean;
import com.xqls.biz.DictionaryBiz;
import com.xqls.biz.MessageBiz;
import com.xqls.dal.model.Dictionary;
import com.xqls.service.MessageService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.VelocityUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class MessageBizImpl implements MessageBiz{

	private static Logger logger = Logger.getLogger(MessageBizImpl.class);
	
	@Autowired private MessageService messageService;
	@Autowired private DictionaryBiz dictionaryBiz;
	
	@Override
	public CommonResult eidt(MessageBean bean){
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			success = messageService.add(bean);
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("编辑短信异常", e);
		}
		return result;
	}
	
	@Override
	public List<Map<String, Object>> queryByMap(Map<String, Object> map){
		List<Map<String, Object>> beanMap = null;
		try {
			beanMap = messageService.queryByMap(map);
		} catch (Exception e) {
			logger.error("根据map查询短信异常",e);
		}
		return beanMap;
	}
	
	@Override
	public CommonResult queryByCommpage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		CommonResult result = new CommonResult();
		try {
			List<Map<String,Object>> messageList=messageService.queryByMap(commonPage.pageToMap());
			int curr=commonPage.getCurr();
			for (int i=0;i<messageList.size();i++){
				if(curr==1){
					messageList.get(i).put("viewId",i+1);
				}else{
					messageList.get(i).put("viewId",i+1+((curr-1)*15));
				}
			}
			commonPage.setPageData(messageList);
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("messageList",commonPage.getPageData());
			Map<Integer, String> alarmTypeMap = dictionaryBiz.queryDictionaryByParent(
					  DictionaryEnums.ALARM_TYPE.getCode());
			showMap.put("alarmTypeMap", alarmTypeMap);
			commonPage.setHtml(VelocityUtil.getHtmlByMap("message/messageListData.vm",showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("分页条件查询短信操作记录异常",e);
		}
		return result;
	}
	
	@Override
	public LevelTimeBean getLevelTime(){
		List<Integer> keyList = getEmunList(true);
		LevelTimeBean bean = new LevelTimeBean();
		try {
			List<Dictionary> dictionary = dictionaryBiz.getListByKeys(keyList);
			if(dictionary.size()>0){
				for (Dictionary dic : dictionary) {
					if(dic.getDictionaryKey() == DictionaryEnums.LEVEL_ONE_TIME.getCode()){
						bean.setLevelOne(Integer.valueOf(dic.getDictionaryValue()));
					}
					if(dic.getDictionaryKey() == DictionaryEnums.LEVEL_TWO_TIME.getCode()){
						bean.setLevelTwo(Integer.valueOf(dic.getDictionaryValue()));
					}
					if(dic.getDictionaryKey() == DictionaryEnums.LEVEL_THREE_TIME.getCode()){
						bean.setLevelThree(Integer.valueOf(dic.getDictionaryValue()));
					}
					int dictionaryKey = dic.getDictionaryKey();
					int intervalTime= DictionaryEnums.INTERVAL_TIME.getCode();
					if(dictionaryKey == intervalTime){
						bean.setIntervalTime(Integer.valueOf(dic.getDictionaryValue()));
					}
					if(dic.getDictionaryKey() == DictionaryEnums.VALUE.getCode()){
						bean.setValve(dic.getDictionaryValue());
					}
					if(dic.getDictionaryKey() == DictionaryEnums.MAINS.getCode()){
						bean.setMains(dic.getDictionaryValue());
					}
					if(dic.getDictionaryKey() == DictionaryEnums.NETWORK.getCode()){
						bean.setNetWork(dic.getDictionaryValue());
					}
					if(dic.getDictionaryKey() == DictionaryEnums.LIQUID_SITE.getCode()){
						bean.setLiquidSite(dic.getDictionaryValue());
					}
					if(dic.getDictionaryKey() == DictionaryEnums.DOOR.getCode()){
						bean.setDoor(dic.getDictionaryValue());
					}
				}
			}
		} catch (Exception e) {
			logger.error("查询报警持续时间异常",e);
		}
		return bean;
	}
	
	@Override
	public CommonResult setLevelTime(LevelTimeBean bean){
		CommonResult result = new CommonResult();
		boolean success = false;
		List<DictionaryBean> beans = new ArrayList<DictionaryBean>();
		try{
			List<Integer> keyList = getEmunList(true);
			List<Object> valueList = getClassList(bean);
			for (int i = 0; i < valueList.size(); i++) {
				DictionaryBean dictionary = new DictionaryBean();
				dictionary.setDictionaryKey(keyList.get(i));
				if(valueList.get(i) != null){
					dictionary.setDictionaryValue(valueList.get(i).toString());
				}else if(valueList.get(3).toString()==null && valueList.get(3).toString().equals("")){
					dictionary.setDictionaryValue("0");
				}else{
					dictionary.setDictionaryValue("no");
				}
				beans.add(dictionary);
			}
			success = editDic(beans);
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("编辑报警持续时间异常",e);
		}
		return result;
	}
	
	@Override
	public boolean judgeLevelType(int levelType){
		List<Integer> levelList = getEmunList(false);
		try {
			if(levelType==DictionaryEnums.NETWORK_OFFLINE.getCode()){
				levelType=DictionaryEnums.NETWORK.getCode();
			}else if(levelType==DictionaryEnums.VALVE_FAILURE.getCode()){
				levelType=DictionaryEnums.VALUE.getCode();
			}else if(levelType==DictionaryEnums.POWER_ANOMALY.getCode()){
				levelType=DictionaryEnums.MAINS.getCode();
			}else if(levelType==DictionaryEnums.DOOR_OPEN.getCode()){
				levelType=DictionaryEnums.DOOR.getCode();
			}else if(levelType==DictionaryEnums.LEVEL_ANOMALY.getCode()){
				levelType=DictionaryEnums.LIQUID_SITE.getCode();
			}
			List<Dictionary> levels = dictionaryBiz.getListByKeys(levelList);
			for (Dictionary dictionary : levels) {
				if(dictionary.getDictionaryKey() == levelType){
					if("yes".equals(dictionary.getDictionaryValue())){
						return true;
					}
				}
			}
		} catch (Exception e) {
			logger.error("批量查询字典异常",e);
		}
		return false;
	}
	
	@Override
	public LevelTimeBean queryLevelTime(){
		List<Integer> list = new ArrayList<Integer>();
		list.add(DictionaryEnums.LEVEL_ONE_TIME.getCode());
		list.add(DictionaryEnums.LEVEL_TWO_TIME.getCode());
		list.add(DictionaryEnums.LEVEL_THREE_TIME.getCode());
		LevelTimeBean bean = new LevelTimeBean();
		try {
			List<Dictionary> timeList = dictionaryBiz.getListByKeys(list);
			for (Dictionary dic : timeList) {
				if(dic.getDictionaryKey() == DictionaryEnums.LEVEL_ONE_TIME.getCode()){
					bean.setLevelOne(Integer.valueOf(dic.getDictionaryValue()));
				}
				if(dic.getDictionaryKey() == DictionaryEnums.LEVEL_TWO_TIME.getCode()){
					bean.setLevelTwo(Integer.valueOf(dic.getDictionaryValue()));
				}
				if(dic.getDictionaryKey() == DictionaryEnums.LEVEL_THREE_TIME.getCode()){
					bean.setLevelThree(Integer.valueOf(dic.getDictionaryValue()));
				}
			}
		} catch (Exception e) {
			logger.error("批量查询字典异常",e);
		}
		return bean;
	}
	
	private List<Integer> getEmunList(boolean isLevel){
		List<Integer> list = new ArrayList<Integer>();
		if(isLevel){
			list.add(DictionaryEnums.LEVEL_ONE_TIME.getCode());
			list.add(DictionaryEnums.LEVEL_TWO_TIME.getCode());
			list.add(DictionaryEnums.LEVEL_THREE_TIME.getCode());
			list.add(DictionaryEnums.INTERVAL_TIME.getCode());
		}
		list.add(DictionaryEnums.VALUE.getCode());
		list.add(DictionaryEnums.MAINS.getCode());
		list.add(DictionaryEnums.NETWORK.getCode());
		list.add(DictionaryEnums.LIQUID_SITE.getCode());
		list.add(DictionaryEnums.DOOR.getCode());
		return list;
	}
	
	private List<Object> getClassList(LevelTimeBean bean){
		List<Object> values = new ArrayList<Object>();
		if(bean!=null){
			values.add(bean.getLevelOne());
			values.add(bean.getLevelTwo());
			values.add(bean.getLevelThree());
			if(bean.getIntervalTime()!=null && !bean.getIntervalTime().equals("")){
				values.add(bean.getIntervalTime());
			}else{
				values.add("0");
			}

			values.add(bean.getValve());
			values.add(bean.getMains());
			values.add(bean.getNetWork());
			values.add(bean.getLiquidSite());
			values.add(bean.getDoor());
		}
		return values;
	}
	
	private boolean editDic(List<DictionaryBean> beans){
		boolean success = false;
		if(beans.size()>0){
			for (DictionaryBean bean : beans) {
				if(bean.getDictionaryValue()!=null && !"".equals(bean.getDictionaryValue())){
					success = dictionaryBiz.editDictionary(bean);
				}
			}
		}
		return success;
	}
}
